package com.rocksea.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rocksea.app.R;
import com.rocksea.app.activity.LowStrainActivity;
import com.rocksea.app.activity.SonicWaveActivity;
import com.rocksea.app.activity.StaticLoadActivity;
import com.rocksea.app.db.UpLoadingTable;
import com.rocksea.app.db.UploadingDbHelper;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.entry.PileInfo;
import com.rocksea.app.utils.PerUtils;
import com.rocksea.app.utils.UiUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by 吴志华 on 2017/9/6.
 * 报检单桩列表
 */

public class PileListAdapter extends RecyclerView.Adapter<PileListAdapter.ViewHolder>
{
    private static final String INFO_EXTRA_KEY = "com.rocksea,app.PileInfoActivity.info";
    private ArrayList<PileInfo>  pileList;
    public Context context;
    public static final int S = 1;
    public static final int Q = 2;

    private int type;

    public PileListAdapter(ArrayList<PileInfo>  pileList, Context context,int type)
    {
        this.pileList = pileList;
        this.context = context;
        this.type = type;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View item = LayoutInflater.from(context).inflate(R.layout.pile_list_item_layout,parent,false);
        return new ViewHolder(item,context);
    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position)
    {

        final PileInfo pile = pileList.get(position);
        if (pile.getPileNo()==null) {
            holder.pileNo.setText("-");
        }else
        {
            holder.pileNo.setText(pile.getPileNo());
        }
        if (type == Q)
        {
            if (pile.getPileTopH()==null) {
                holder.tvPileHigh.setText("-");
            }else
            {
                holder.tvPileHigh.setText(pile.getPileTopH());
            }

        }
        else
        {
            holder.tvPileHigh.setVisibility(View.GONE);
        }

        if (type==Q)
        {
            if (pile.getPileBottomH()==null)
            {
                holder.tvPileBottom.setText("-");
            }else
            {
                holder.tvPileBottom.setText(pile.getPileBottomH());
            }
        }
        else
        {
            holder.tvPileBottom.setVisibility(View.GONE);
        }


        if (pile.getLoadTime()==0)
        {
            holder.tvLoadTime.setText("-");
            holder.upLoadState.setImageResource(R.mipmap.ic_upload_action);

        }
        else
        {
            holder.upLoadState.setImageResource(R.mipmap.ic_upload_ok);
            Date date = new Date(pile.getLoadTime());
            String[] loadTimes = PerUtils.dateToStr(date).split(" ");
            String loadTime = String.format("%s \n%s",loadTimes[0],loadTimes[1]);

            holder.tvLoadTime.setText(loadTime);
        }
        if (pile.getReceiveTime()!=null)
        {
            holder.itemView.setBackground(ContextCompat.getDrawable(context,R.drawable.uploaded_background));
            String[] times = pile.getReceiveTime().split(" ");
            if (times.length==1)
                holder.tvReceviceTime.setText(pile.getReceiveTime());
            else
            {
                String time = String.format("%s \n%s",times[0],times[1]);
                holder.tvReceviceTime.setText(time);
            }
        }
        else
        {
            holder.tvReceviceTime.setText("-");
            holder.pileNo.setTextColor(ContextCompat.getColor(context,R.color.gark_60));
            holder.tvLoadTime.setTextColor(ContextCompat.getColor(context,R.color.gark_60));
            holder.tvPileHigh.setTextColor(ContextCompat.getColor(context,R.color.gark_60));
            holder.tvPileBottom.setTextColor(ContextCompat.getColor(context,R.color.gark_60));
            holder.tvReceviceTime.setTextColor(ContextCompat.getColor(context,R.color.gark_60));

        }

        holder.upLoadState.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                if (pile.getReceiveTime() == null || pile.getReceiveTime().equals(""))
                {
                    UiUtils.showToast(String.format("桩号%s还没有检测",pile.getPileNo()));
                    return;
                }

                if (pile.getLoadTime() > 0 && pile.getDataType() != HistoryTable.TEST_DY)
                {
                    UiUtils.showToast(String.format("桩号%s已上传",pile.getPileNo()));
                    return;
                }
                // 添加到上传队列中
                UpLoadingTable upLoadingTable = new UpLoadingTable();
                upLoadingTable.baseInfoId = pile.getBasicInfoId();
                upLoadingTable.mode = pile.getDataType();
                upLoadingTable.pileNo = pile.getPileNo();
                upLoadingTable.planNum = pile.getSerilNo();
                upLoadingTable.uuid = UpLoadingTable.DEFAULT_UUID;
                if (UploadingDbHelper.insert(upLoadingTable) == 0)
                {
                    UiUtils.showToast("已添加到了上传队列");
                }
                else
                {
                    UiUtils.showToast("上传队列已存在");
                }

            }
        });

        holder.vItemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                int mode = pileList.get(position).getDataType();
                String baseInfoId = pileList.get(position).getBasicInfoId();
                String serialNo = pileList.get(position).getSerilNo();
                String pileNo = pileList.get(position).getPileNo();
                Intent intent = null;
                switch (mode)
                {
                    case HistoryTable.TEST_DY:
                        intent = new Intent(context, LowStrainActivity.class);
                        break;
                    case HistoryTable.TEST_GY:
                        intent = new Intent(context, LowStrainActivity.class);
                        break;
                    case HistoryTable.TEST_JZ:
                        intent = new Intent(context, StaticLoadActivity.class);
                        break;
                    case HistoryTable.TEST_SC:
                        intent = new Intent(context, SonicWaveActivity.class);
                        break;
                }
                if (intent!=null)
                {
                    String[] pileInfos = new String[]{serialNo,pileNo,baseInfoId};
                    intent.putExtra(INFO_EXTRA_KEY,pileInfos);
                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount()
    {
        return pileList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public TextView pileNo;
        public TextView tvPileHigh;
        public TextView tvPileBottom;
        public TextView tvReceviceTime;
        public TextView tvLoadTime;
        public ImageView upLoadState;
        public View vItemView;

        public ViewHolder(View itemView,Context context)
        {
            super(itemView);
            itemView.setBackground(ContextCompat.getDrawable(context,R.drawable.item_selecter_layout));
            pileNo = (TextView) itemView.findViewById(R.id.tvPileNo);
            tvPileHigh = (TextView) itemView.findViewById(R.id.tvPileHigh);
            tvPileBottom = (TextView) itemView.findViewById(R.id.tvPileBottom);
            tvReceviceTime = (TextView) itemView.findViewById(R.id.tvReceviceTime);
            tvLoadTime = (TextView) itemView.findViewById(R.id.tvLoadTime);
            upLoadState = (ImageView) itemView.findViewById(R.id.upLoadState);
            vItemView = itemView.findViewById(R.id.ItemView);
        }
    }
}
